package artid.com.arttogo.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class PicDotView extends View {
	/**
	 * 环的宽度
	 */
	final int RING_WIDTH = 2;
	/**
	 * 圆的直径
	 */
	final int RING_CIRCLE = 5;
	private final Paint paint;
	private final Context context;
	List<Point> listDot;
	/**
	 * click范围控制,单位dp
	 */
	int CLICK_SENCE = 20;
	int i = 0;
	int innerCircle, ringWidth;
	onDotClickListener listener;
	int width, height;
	Bitmap mBitmap = null;
	Boolean isRound = true;

	public void setIsRound(Boolean isRound) {
		this.isRound = isRound;
	}

	public PicDotView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.paint = new Paint();
		this.paint.setAntiAlias(true); // 消除锯齿
		this.paint.setStyle(Paint.Style.STROKE); // 绘制空心圆
		listDot = new ArrayList<Point>();
		if (isInEditMode()) {
			return;
		}
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		width = dm.widthPixels;// 宽度
		height = dm.heightPixels;// 高度
		innerCircle = dip2px(context, RING_CIRCLE); // 设置内圆半径
		ringWidth = dip2px(context, RING_WIDTH); // 设置圆环宽度
		// TODO Auto-generated constructor stub
	}

	// @SuppressLint("WrongCall")
	// public Bitmap getDotBitmap(Bitmap inBit){
	// // Bitmap bitmap = Bitmap.createBitmap(inBit.getWidth(),
	// inBit.getHeight(), Config.ARGB_8888);
	// Canvas canvas = new Canvas(inBit);
	// onDraw(canvas);
	// return inBit;
	// }

	public void resetView() {
		listDot.clear();
	}

	public void addDotXy(int x, int y, String url) {
		Point point = new Point(x, y, url);
		listDot.add(point);
	}

	public void addDotXy(String x, String y, double picScrn, String url) {
		int x1 = Integer.valueOf(x);
		int x_result = (int) ((double) x1 / picScrn);
		int y1 = Integer.valueOf(y);
		int y_result = (int) ((double) y1 / picScrn);
		addDotXy(x_result, y_result, url);
	}

	public interface onDotClickListener {
		void onClick(int i);
	}

	public void setOnClick(onDotClickListener listener) {

		this.listener = listener;

	}

	@Override
	protected void onDraw(Canvas canvas) {

		if (!listDot.isEmpty()) {
			for (int i = 0; i < listDot.size(); i++) {
				index = (i + 1);
				Point item = listDot.get(i);
				if (!isRound) {
					drawRndRect(canvas, item.x, item.y);
				} else {
					drawDot(canvas, item.x, item.y);
				}

			}

		}
		super.onDraw(canvas);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int x = (int) event.getX();
		int y = (int) event.getY();
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:

			for (int i = 0; i < listDot.size(); i++) {
				Point item = listDot.get(i);
				if (Math.abs(x - item.x) < dip2px(context, CLICK_SENCE)
						&& Math.abs(y - item.y) < dip2px(context, CLICK_SENCE)) {
					this.i = i;
					listener.onClick(i);
					return true;
				}
			}

			break;
		}

		return true;
	}

	int index = 1;

	void drawRndRect(Canvas canvas, int x, int y) {
		RectF oval3 = new RectF(x - innerCircle, y - innerCircle, x
				+ innerCircle, y + innerCircle);// 设置个新的长方形

		paint.setAntiAlias(true);
		this.paint.setStyle(Paint.Style.STROKE);
		this.paint.setARGB(155, 230, 0, 0);
		this.paint.setStrokeWidth(ringWidth);
		canvas.drawRect(oval3, this.paint);
		drawText(canvas, x, y);
	}

	void drawDot(Canvas canvas, int x, int y) {

		// 绘制内圆
		this.paint.setARGB(155, 230, 0, 0);
		this.paint.setStrokeWidth(2);
		canvas.drawCircle(x, y, innerCircle, this.paint);

		// 绘制圆环
		this.paint.setARGB(155, 230, 0, 0);
		this.paint.setStrokeWidth(ringWidth);
		canvas.drawCircle(x, y, innerCircle + 1 + ringWidth / 2, this.paint);

		// 绘制外圆
		this.paint.setARGB(155, 230, 0, 0);
		this.paint.setStrokeWidth(2);
		canvas.drawCircle(x, y, innerCircle + ringWidth, this.paint);
		drawText(canvas, x, y);

		// canvas.drawText(String.valueOf(index), x + innerCircle + ringWidth, y
		// - innerCircle - ringWidth, paint);
		// index++;
	}

	void drawText(Canvas canvas, int x, int y) {
		paint.setTextSize(dip2px(context, 20));

		// 点位于最右侧
		if (x > (width - innerCircle * 3)) {
			canvas.drawText(String.valueOf(index), x - (innerCircle * 6), y,
					paint);
		}
		// 点位于最左侧 1
		else if (x < (innerCircle * 3)) {
			canvas.drawText(String.valueOf(index), x + (innerCircle * 3), y
					+ (innerCircle * 3), paint);
		}
		// 点位于上侧
		else if (y < (innerCircle * 3)) {
			canvas.drawText(String.valueOf(index), x, y + (innerCircle * 6),
					paint);
		} else {
			canvas.drawText(String.valueOf(index), x + innerCircle + ringWidth,
					y - innerCircle - ringWidth, paint);
		}
	}

	class Point {
		int x;
		int y;
		String url;

		public Point(int x, int y, String url) {
			// TODO Auto-generated constructor stub
			this.x = x;
			this.y = y;
			this.url = url;
		}

		public int getX() {
			return x;
		}

		public void setX(int x) {
			this.x = x;
		}

		public int getY() {
			return y;
		}

		public void setY(int y) {
			this.y = y;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		;
		return (int) (dpValue * scale + 0.5f);
	}
}
